package com.mlink.vs.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.seasar.struts.annotation.Execute;

import com.google.gson.Gson;
import com.mlink.vs.entity.PiccMsgqueue;
import com.mlink.vs.entity.PiccMsgsendLog;
import com.mlink.vs.service.DbTimeService;
import com.mlink.vs.service.PiccMsgqueueService;
import com.mlink.vs.service.PiccMsgsendLogService;
import com.mlink.vs.util.Constants;
import com.mlink.vs.util.StreamUtil;
import com.mlink.vs.util.jgpush;

public class MsgAction {

	/**
	 * request请求对象
	 */
	@Resource
	private HttpServletRequest req;

	/**
	 * response请求对象
	 */
	@Resource
	private HttpServletResponse rep;
	
	@Resource
	DbTimeService dbTimeService;
	
	@Resource
	PiccMsgqueueService piccMsgqueueService;
	
	@Resource
	PiccMsgsendLogService piccMsgsendLogService;
	
	//坐席端推送接口
	@Execute(validator = false)
	public String msgSend() {
		Gson gson = new Gson();
		StringBuffer indata = StreamUtil.getRequestBody(req);
		Map<String, String> stsMap = new HashMap<String, String>();

		Map<String, String> mapData = gson.fromJson(indata.toString(), Map.class);
		if (mapData == null)
			return null;
		
		String customerName = (String) mapData.get("customerName");
		String customerTel = (String) mapData.get("customerTel");
		String lon = (String) mapData.get("lon");
		String lat = (String) mapData.get("lat");
		String caseNumber = (String) mapData.get("caseNumber");
		String carNumber = (String) mapData.get("carNumber");
		String caseType = (String) mapData.get("caseType");
		String leadingUserId = (String) mapData.get("leadingUserId");
		String address = (String) mapData.get("address");

		try {
			rep.setCharacterEncoding("utf-8");
			if (StringUtils.isBlank(customerName)) {
				stsMap.put("sts", "2");
				stsMap.put("msg", "customerName不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			if (StringUtils.isBlank(customerTel)) {
				stsMap.put("sts", "2");
				stsMap.put("msg", "customerTel不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			if (StringUtils.isBlank(caseNumber)) {
				stsMap.put("sts", "2");
				stsMap.put("msg", "caseNumber不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			if (StringUtils.isBlank(carNumber)) {
				stsMap.put("sts", "2");
				stsMap.put("msg", "carNumber不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			if (StringUtils.isBlank(caseType)) {
				stsMap.put("sts", "2");
				stsMap.put("msg", "caseType不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			if (StringUtils.isBlank(leadingUserId)) {
				stsMap.put("sts", "2");
				stsMap.put("msg", "leadingUserId不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			if (StringUtils.isBlank(address)) {
				stsMap.put("sts", "2");
				stsMap.put("msg", "address不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			String alertContent="你有新的推送订单啦";
			String title="999极光推送";
			String registrationId="100d85590942907083e";
			int statusCode = jgpush.sendPush_fromRegistrationId_Message(alertContent,title,registrationId,mapData);
			if(statusCode==0){
				PiccMsgsendLog piccMsgsendLog = new PiccMsgsendLog();
				piccMsgsendLog.msgId=UUID.randomUUID().toString().replace("-", "");
				piccMsgsendLog.customerName=customerName;
				piccMsgsendLog.customerTel=customerTel;
				piccMsgsendLog.lon=lon;
				piccMsgsendLog.lat=lat;
				piccMsgsendLog.caseNumber=caseNumber;
				piccMsgsendLog.carNumber=carNumber;
				piccMsgsendLog.caseType=caseType;
				piccMsgsendLog.leadingUserId=leadingUserId;
				piccMsgsendLog.sentStatus=Constants.PUSH_SUCCESS_STATE;
				piccMsgsendLog.createTime=dbTimeService.getDbTimeStamp();
				piccMsgsendLog.address=address;
				piccMsgsendLogService.insert(piccMsgsendLog);
				stsMap.put("sts", "0");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
			}else{
				PiccMsgsendLog piccMsgsendLog = new PiccMsgsendLog();
				piccMsgsendLog.msgId=UUID.randomUUID().toString().replace("-", "");
				piccMsgsendLog.customerName=customerName;
				piccMsgsendLog.customerTel=customerTel;
				piccMsgsendLog.lon=lon;
				piccMsgsendLog.lat=lat;
				piccMsgsendLog.caseNumber=caseNumber;
				piccMsgsendLog.carNumber=carNumber;
				piccMsgsendLog.caseType=caseType;
				piccMsgsendLog.leadingUserId=leadingUserId;
				piccMsgsendLog.sentStatus=Constants.PUSH_ERROR_STATE;
				piccMsgsendLog.createTime=dbTimeService.getDbTimeStamp();
				piccMsgsendLog.address=address;
				piccMsgsendLogService.insert(piccMsgsendLog);
				//推送失败 再往失败表里添加记录
				PiccMsgqueue piccMsgqueue = new PiccMsgqueue();
				piccMsgqueue.msgId=UUID.randomUUID().toString().replace("-", "");
				piccMsgqueue.customerName=customerName;
				piccMsgqueue.customerTel=customerTel;
				piccMsgqueue.lon=lon;
				piccMsgqueue.lat=lat;
				piccMsgqueue.caseNumber=caseNumber;
				piccMsgqueue.carNumber=carNumber;
				piccMsgqueue.caseType=caseType;
				piccMsgqueue.leadingUserId=leadingUserId;
				piccMsgqueue.createTime=dbTimeService.getDbTimeStamp();
				piccMsgqueue.address=address;
				piccMsgqueueService.insert(piccMsgqueue);
				
				stsMap.put("sts", "1");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();

			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
	
}